package lsl.leetCode.year2023.test0201;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author Zed
 * @description
 * @date 2023/2/1 21:25
 */
public class test349 {
    public static void main(String[] args) {
        int[] nums1 = {1, 2, 2, 1};
        int[] nums2 = {2, 2};
        intersection(nums1, nums2);
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int[] arr = new int[nums1.length];
        int count = 0;
        for (int i = 0; i < nums1.length; i++) {
            map.put(nums1[i], map.getOrDefault(nums1[i], 0) + 1);
            for (int j = 0; j < nums2.length; j++) {
                if (map.getOrDefault(nums2[j], 0) == 1) {
                    arr[count++] = nums2[j];
                    map.put(nums2[j], map.getOrDefault(nums2[j], 0) + 1);
                }
            }
        }
        return Arrays.copyOfRange(arr, 0, count);
    }
}
